package com.tarena.lbs.business.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.pojo.param.StoreChainParam;
import com.tarena.lbs.business.pojo.query.StoreChainQuery;
import com.tarena.lbs.business.pojo.vo.StoreChainVO;
import com.tarena.lbs.business.servcie.StoreChainService;
import com.tarena.lbs.business.servcie.impl.QRCodeUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

@Tag(name = "店铺相关功能接口")
@RestController
@Slf4j
public class StoreChainController {
    @Autowired
    private StoreChainService storeChainService;

    @Operation(summary = "查询二维码列表")
    @PostMapping("/admin/basic/chain/getAll")
    public Result<PageResult<StoreChainVO>> pageList(@RequestBody StoreChainQuery query){
        log.debug("查询二维码列表,参数:{}",query);
        log.debug("Controller层查询二维码门店名称,参数:{}",query.getStoreName());
        log.debug("Controller层查询二维码门店区域,参数:{}",query.getArea());
        PageResult<StoreChainVO> pageResult = storeChainService.pageList(query);
        return new Result<>(pageResult);
    }

    @Operation(summary = "启用二维码")
    @PostMapping("/admin/basic/chain/qrcodeStatus")
    public Result<Void> update(@RequestBody StoreChainParam param)
            throws BusinessException {
        storeChainService.update(param);
        log.debug("启用二维码,参数:{}",param);
        return Result.success();
    }

    @Operation(summary = "查看店铺二维码")
    @PostMapping("/admin/basic/chain/qrcode-with-logo")
    public ResponseEntity<byte[]> generateQRCodeWithLogo(@RequestBody StoreChainQuery query) {
        try {
            // 读取logo图片
            URL url = new URL(query.getLinkImage());
            URLConnection conn = url.openConnection();
            InputStream stream = conn.getInputStream();
            BufferedImage image = ImageIO.read(stream);

            byte[] imageData = QRCodeUtil.generateQRCodeWithLogo(query.getLink(),image);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_PNG);

            return new ResponseEntity<>(imageData, headers, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
